package com.jqweb.controller.Shopping;

import com.jqweb.controller.Shopping.pojo.AddCartDto;
import com.jqweb.entity.ShopEntity;
import com.jqweb.pojo.Result;
import com.jqweb.services.ShoppingService;
import com.jqweb.utils.JWT;
import com.jqweb.utils.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class shoppingController {
    @Autowired
    private ShoppingService shoppingService;



    // 分页获取商品信息
    @PostMapping("/getAll")
    public Result getAll(@RequestBody Paging paging) {
        Map<String, Object> data = shoppingService.getAll(paging);
        return Result.success("获取成功", data);
    }

    // 根据id 获取商品信息
    @PostMapping("/get/{id}")
    public Result get(@PathVariable Integer id) {
        ShopEntity shopEntity = shoppingService.getById(id);
        if (shopEntity != null){
            return Result.success("获取成功", shopEntity);
        }
        return Result.error("获取失败");
    }

    // 新增商品信息
    @PostMapping("/create")
    public Result create(@RequestBody ShopEntity shopEntity) {
        Integer i =  shoppingService.createShop(shopEntity);
        if (i > 0){
            return Result.success("新增成功", null);
        }
        return Result.error("新增失败");
    }

    // 修改商品信息
    @PostMapping("/update")
    public Result update(@RequestBody ShopEntity shopEntity) {
        Integer i =  shoppingService.updateShop(shopEntity);
        if (i > 0){
            return Result.success("修改成功", null);
        }
        return Result.error("获取失败");
    }

    // 删除商品信息
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Integer i =  shoppingService.deleteShop(id);
        if (i > 0){
            return Result.success("删除成功", null);
        }
        return Result.error("获取失败");
    }

    // 根据用户id查询购物车信息
    @PostMapping("/cart")
    public Result getCart(@RequestBody Paging paging,@RequestHeader(value = "Authorization", required = false) String token) {
        Object id = JWT.getClaimValue(token,"id");
        if (id == null){
            return Result.error("token失效");
        }
        Map<String, Object> data = shoppingService.getCartListById(paging,(String) id);
        if (data != null){
            return Result.success("获取成功", data);
        }
        return Result.success("获取失败",null);
    }

    // 购物车添加商品
    @PostMapping("/cart/add")
    public Result addCart(@RequestBody AddCartDto data, @RequestHeader(value = "Authorization", required = false) String token) {
        Object id = JWT.getClaimValue(token,"id");
        if (id == null){
            return Result.error("token失效");
        }
        Integer shop = shoppingService.addCart(data,(String) id);
        if (shop != null){
            return Result.success("添加成功", null);
        }
        return Result.success("添加失败",null);
    }

    // 根据id更新单个购物车数据
    @PostMapping("/cart/update/{id}")
    public Result updateCart(@PathVariable Integer id,@RequestBody AddCartDto data, @RequestHeader(value = "Authorization", required = false) String token) {
        Object id1 = JWT.getClaimValue(token,"id");
        if (id1 == null){
            return Result.error("token失效");
        }
        Integer shop = shoppingService.updateCart(id,data,(String) id1);
        if (shop != null){
            return Result.success("修改成功",null);
        }
        return Result.success("修改失败",null);
    }

    // 根据id数组删除购物车数据(多个或者一个)
    @PostMapping("/cart/delete")
    public Result deleteCart(@RequestBody List<Integer> idList, @RequestHeader(value = "Authorization", required = false) String token) {
        Object id = JWT.getClaimValue(token,"id");
        if (id == null){
            return Result.error("token失效");
        }
        Integer shop = shoppingService.deleteCart(idList);
        if (shop != null){
            return Result.success("删除成功",null);
        }
        return Result.success("删除失败",null);
    }
}
